package com.atguigu.ucenter_member.controller;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.jwt.JwtUtils;
import com.atguigu.ucenter_member.entity.UcenterMemberEntity;
import com.atguigu.ucenter_member.entity.vo.RegisterVo;
import com.atguigu.ucenter_member.service.UcenterMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 会员表
 *
 * @author wuyao
 * @email 13918471632@163.com
 * @date 2021-03-23 12:12:26
 */
@RestController
@RequestMapping("/edu_ucenter/ucentermember")
@CrossOrigin
@Api(tags = "--前台用户注册评论信息管理接口API")
public class UcenterMemberController {

    /**
     *
     * @return  token
     */
    @Autowired
    private UcenterMemberService ucenterMemberService;

    @ApiOperation(value = "用户登录",notes = "返回token")
    @PostMapping("/login")
    public R login(@RequestBody UcenterMemberEntity ucenterMemberEntity){
        String token =ucenterMemberService.login(ucenterMemberEntity);
        return R.ok().data("token",token);
    }
    @ApiOperation(value="登录根据token获取用户信息")
    @GetMapping("/authToken")
    public R authToken(HttpServletRequest request){
        String memberId=JwtUtils.getMemberIdByJwtToken(request);
        UcenterMemberEntity ucenterMemberEntity=ucenterMemberService.getInfoByToken(memberId);
        return R.ok().data("item",ucenterMemberEntity);
    }


    @ApiOperation(value="用户注册")
    @PostMapping("/register")
    public R register (@RequestBody RegisterVo registerVo){
        ucenterMemberService.insert(registerVo);
        return R.ok();
    }

    @ApiOperation(value="根据id获取用户")
    @GetMapping("/getUserById/{id}")
    public R getUserById(@PathVariable String id){
        UcenterMemberEntity memeberEntity = ucenterMemberService.getById(id);
        return R.ok().data("memeberEntity",memeberEntity);
    }
    @ApiOperation(value="根据id获取用户,消费者")
    @GetMapping("/getUserInfoByMemberId/{memberId}")
    public com.atguigu.commonutils.ordervo.UcenterMemberEntity getUserInfoByMemberId(@PathVariable String memberId){
        UcenterMemberEntity memeberEntity = ucenterMemberService.getById(memberId);
        com.atguigu.commonutils.ordervo.UcenterMemberEntity ucenterMemberEntity=new com.atguigu.commonutils.ordervo.UcenterMemberEntity();
        BeanUtils.copyProperties(memeberEntity,ucenterMemberEntity);
        return ucenterMemberEntity;
    }
}
